package com.suanfa.string;

/**
 * Created by buguniao on 16/3/6.
 */
public class Test {
    public static void main(String[] args){
        String s="我们是共产主义的接班人我们";
        String t="的接";

        System.out.println(findStrIndex(s,t,8));
    }

    //查找字符串，存在返回字符串的位置
    public static int findStrIndex(String s,String t,int pos){
        char[] s1=s.toCharArray();
        char[] s2=t.toCharArray();
        int i=pos,j=0;
        while(i<s1.length && j < s2.length){

            if(s1[i] == s2[j]){
                i++;
                j++;
            }else{
                i=i-j+1;
                j=0;
            }
        }

        if(j>=s2.length){
            return i-j;
        }
        return 0;
    }

}
